# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from fastapi import APIRouter, Request, Depends
from hypertext import R, response_json
from apps.api.schemas import login_schema as schema
from apps.api.service.login_service import LoginService

router = APIRouter(prefix="/login", tags=["登录接口"])


@router.post("/register", summary="账号注册", response_model=R[schema.LoginTokenVo])
@response_json
async def register(request: Request, params: schema.RegisterIn):
    terminal: int = request.state.terminal
    return await LoginService.register(params, terminal)


@router.post("/account_login", summary="账号登录", response_model=R[schema.LoginTokenVo])
@response_json
async def account_login(request: Request, params: schema.AccountLoginIn):
    terminal: int = request.state.terminal
    return await LoginService.account_login(
        params.account,
        params.password,
        terminal
    )


@router.post("/mobile_login", summary="手机登录", response_model=R[schema.LoginTokenVo])
@response_json
async def mobile_login(request: Request, params: schema.MobileLoginIn):
    terminal: int = request.state.terminal
    return await LoginService.mobile_login(
        params.mobile,
        params.code,
        terminal
    )


@router.post("/oa_login", summary="公众号授权登录", response_model=R[schema.LoginTokenVo])
@response_json
async def oa_login(request: Request, params: schema.OaLoginIn):
    terminal: int = request.state.terminal
    return await LoginService.oa_login(
        params.state,
        params.code,
        terminal
    )


@router.get("/qrcode", summary="微信登录二维码", response_model=R[schema.LoginQrcodeVo])
@response_json
async def qrcode(params: schema.OaQrcodeIn = Depends()):
    return await LoginService.qrcode(params.event)


@router.get("/ticket", summary="微信登录扫码检测", response_model=schema.LoginTicketVo)
@response_json
async def ticket(params: schema.ScanLoginIn = Depends()):
    return await LoginService.ticket(params.state)


@router.post("/logout", summary="退出系统", response_model=R)
@response_json
async def logout(request: Request):
    token = request.headers.get("Authorization", "")
    return await LoginService.logout(token.strip("Bearer "))
